package br.com.roteador.controller;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Result;
import br.com.roteador.component.AcessSession;

@Controller
public class HomeController extends BasicController {
	@Inject AcessSession acessSession;
	@Inject private HttpSession session;
	
	@Inject
    public HomeController(Result result, ResourceBundle bundle) {
		this.result = result;
		this.bundle = bundle;
	}
	
	public HomeController(){}
	
	@PostConstruct
    private void init(){
    	injetaUsuario();
    }
	
    @Path(value = "/")
	public void index() {}
    
    @Path(value = "/login")
	public void login() {
    }
    
    @Path(value = "/changeLanguage/{language}")
    public void changeLanguage(String language){
    	if(language.equals("en_us")){
    		session.setAttribute("locale", "en_US");
    		Config.set(session, Config.FMT_LOCALE, new Locale("en", "US"));
    	}
    	if(language.equals("pt_br")){
    		session.setAttribute("locale", "pt_BR");
    		Config.set(session, Config.FMT_LOCALE, new Locale("pt", "BR"));
    	}
    	if(language.equals("es_es")){
    		session.setAttribute("locale", "es_es");
    		Config.set(session, Config.FMT_LOCALE, new Locale("es", "ES"));
    	}
    	redirect();
	}
    
    @Path(value = "/logout")
	public void logout() {
    	acessSession.setNome(null);
    	result.redirectTo(HomeController.class).login();
    }

	protected void redirect() {
		result.redirectTo(HomeController.class).index();
	}

}